package com.example.CarManagement.sys.controller;

import com.example.CarManagement.common.Result;
import com.example.CarManagement.enumPojo.ApplicationStatus;
import com.example.CarManagement.sys.dto.ApplicationsDto;
import com.example.CarManagement.sys.dto.DispatchDto;
import com.example.CarManagement.sys.entity.Applications;
import com.example.CarManagement.sys.entity.Approvals;
import com.example.CarManagement.sys.service.impl.ApplicationsServiceImpl;
import com.example.CarManagement.sys.service.impl.ApprovalsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 用车申请表，记录用户提交的用车申请及审批状态 前端控制器
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-17
 */
@RestController
@RequestMapping("/sys/applications")
public class ApplicationsController {


    @Autowired
    private ApplicationsServiceImpl applicationsService;


    // 添加
    @PreAuthorize("@se.hasPermi('APPLICATIONS_INSERT')")
    @PostMapping("/add")
    public Result<?> add(@RequestBody Applications applications, @RequestHeader("Authorization") String token) {

        return applicationsService.add(applications, token);
    }


    // 删除
    @PreAuthorize("@se.hasPermi('APPLICATIONS_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable String id) {
        return Result.success(applicationsService.removeById(id));
    }

    // 修改
    @PreAuthorize("@se.hasPermi('APPLICATIONS_UPDATE')")
    @PutMapping("/update")
    public Result<?> update(@RequestBody Applications applications) {
        return Result.success(applicationsService.updateById(applications));
    }

    // 取消申请
    @PreAuthorize("@se.hasPermi('APPLICATIONS_UPDATE')")
    @PutMapping("/{id}/cancel")
    public Result<?> cancel(@PathVariable String id) {

        return applicationsService.cancel(id);

    }

    // 更具当前用户自己的所有申请（小程序端）
    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @GetMapping("/selfList")
    public Result<?> selfList(@RequestHeader("Authorization") String token) {
        return applicationsService.selfList(token);
    }

    // 查询当前用户需要审核的所有申请
    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @GetMapping("/needApproveList")
    public Result<?> needApproveList(@RequestHeader("Authorization") String token, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam(required = false) String applicationNumber, @RequestParam(required = false) String status) {
        return applicationsService.needApproveList(token, pageNo, pageSize, applicationNumber, status);
    }

    // 审核订单
    @PreAuthorize("@se.hasPermi('APPLICATIONS_REVIEW')")
    @PostMapping("/review")
    public Result<?> review(@RequestHeader("Authorization") String token, @RequestBody ApplicationsDto applicationsDto) {
        Applications applications = applicationsDto.getApplications();
        Approvals approvals = applicationsDto.getApprovals();

        return applicationsService.review(applications, approvals, token);
    }

    // 用户开始订单
    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @PutMapping("/start/{id}")
    public Result<?> start(@PathVariable String id) {
        return applicationsService.start(id);
    }


    // 更具id查询订单详细
    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @GetMapping("/detail/{id}")
    public Result<?> detail(@PathVariable String id) {
        return applicationsService.detail(id);
    }

    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @GetMapping("/userAppDetail/{id}")
    public Result<?> userAppDetail(@PathVariable String id) {
        return applicationsService.userAppDetail(id);
    }

    // 派单调度查询
    @PreAuthorize("@se.hasPermi('APPLICATIONS_SELECT')")
    @GetMapping("/dispatchList")
    public Result<?> dispatchList(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam(required = false) String applicationNumber) {
        return applicationsService.dispatchList(pageNo, pageSize, applicationNumber);
    }


    // 调度
    @PreAuthorize("@se.hasPermi('APPLICATIONS_DISPATCH')")
    @PostMapping("/dispatch")
    public Result<?> dispatch(@RequestBody DispatchDto dispatchDto, @RequestHeader("Authorization") String token) {
        return applicationsService.dispatch(dispatchDto, token);
    }
}
